package com.hm.service.api.main;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hm.service.framework.PostController;
import com.hm.service.service.main.TaskHandleService;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping(value = "/MAIN", produces = {"application/json;charset=UTF-8"})
public class SZCG_000_000_001 extends PostController {
    private static final Logger logger = LoggerFactory.getLogger(SZCG_000_000_001.class);

    private final TaskHandleService taskService;

    public SZCG_000_000_001(TaskHandleService taskService) {
        this.taskService = taskService;
    }


    //任务管理接口
    @PostMapping(value = "/000_000_001")
    public String doPost(HttpServletRequest request) {
        return handleRequest(request);
    }

    @Override
    protected String postRequest(HashMap<String, String> param) {
        //解析json
        String requestBody = param.get("requestBody");
        JSONObject jsonObject = (JSONObject) JSON.parse(requestBody);
        String taskId = jsonObject.getString("taskId");
        String action = jsonObject.getString("action");
        String cronId = jsonObject.getString("cronId");
        String description = jsonObject.getString("description");
        //判断动作
        return switch (action) {
            case "0" -> taskService.startTask(taskId, cronId, description); //启动任务
            case "1" -> taskService.deleteTask(taskId);//删除任务
            case "2" -> taskService.editTask(taskId, cronId);//修改任务
            case "3" -> taskService.queryAllTask(); //查询所有任务
            default -> generateFail("action错误");
        };
    }
}
